package huangjie;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Zonghezuoy {

    // 生成中奖号码
    private static int[] generateWinningNumbers() {
        int[] numbers = new int[7];
        Set<Integer> redBalls = new HashSet<>();
        Random random = new Random();

        // 生成6个不重复的红球号码（1 - 33）
        while (redBalls.size() < 6) {
            redBalls.add(random.nextInt(33) + 1);
        }

        int index = 0;
        for (Integer num : redBalls) {
            numbers[index++] = num;
        }

        // 生成1个蓝球号码（1 - 16）
        numbers[6] = random.nextInt(16) + 1;

        return numbers;
    }

    // 获取用户输入的号码
    private static int[] getUserNumbers() {
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[7];
        Set<Integer> redBalls = new HashSet<>();

        System.out.println("请输入6个红球号码（1 - 33，不重复）：");
        for (int i = 0; i < 6; ) {
            System.out.print("第" + (i + 1) + "个红球：");
            if (scanner.hasNextInt()) {
                int num = scanner.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("请输入1 - 33之间的数字！");
                } else if (redBalls.contains(num)) {
                    System.out.println("号码重复，请重新输入！");
                } else {
                    redBalls.add(num);
                    numbers[i] = num;
                    i++;
                }
            } else {
                System.out.println("输入无效，请重新输入数字！");
                scanner.next(); // 清除无效输入
            }
        }

        System.out.print("请输入蓝球号码（1 - 16）：");
        while (true) {
            if (scanner.hasNextInt()) {
                int num = scanner.nextInt();
                if (num >= 1 && num <= 16) {
                    numbers[6] = num;
                    break;
                }
                System.out.println("请输入1 - 16之间的数字！");
            } else {
                System.out.println("输入无效，请重新输入数字！");
                scanner.next();
            }
        }

        return numbers;
    }

    // 检查中奖结果
    private static void checkResult(int[] winning, int[] user) {
        int[] winRed = Arrays.copyOf(winning, 6);
        int[] userRed = Arrays.copyOf(user, 6);
        int winBlue = winning[6];
        int userBlue = user[6];

        int redMatch = 0;
        Set<Integer> winRedSet = new HashSet<>();
        for (int num : winRed) {
            winRedSet.add(num);
        }
        for (int num : userRed) {
            if (winRedSet.contains(num)) {
                redMatch++;
            }
        }

        boolean blueMatch = (winBlue == userBlue);

        System.out.println("\n红球命中数：" + redMatch);
        System.out.println("蓝球" + (blueMatch ? "命中" : "未命中"));

        String prizeLevel = "未中奖";
        if (redMatch == 6 && blueMatch) {
            prizeLevel = "一等奖";
        } else if (redMatch == 6) {
            prizeLevel = "二等奖";
        } else if (redMatch == 5 && blueMatch) {
            prizeLevel = "三等奖";
        } else if (redMatch == 5 || (redMatch == 4 && blueMatch)) {
            prizeLevel = "四等奖";
        } else if (redMatch == 4 || (redMatch == 3 && blueMatch)) {
            prizeLevel = "五等奖";
        } else if (blueMatch) {
            prizeLevel = "六等奖";
        }

        System.out.println("中奖结果：" + prizeLevel);
    }

    public static void main(String[] args) {
        int[] winningNumbers = generateWinningNumbers();
        System.out.println("本期中奖号码：" + Arrays.toString(winningNumbers));

        int[] userNumbers = getUserNumbers();

        checkResult(winningNumbers, userNumbers);
    }
}